package com.future.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.future.dao.DepartmentDAO;
import com.future.dao.InforRateDAO;
import com.future.dao.SecondFillBlankDAO;
import com.future.dao.UserDAO;
import com.future.model.bo.InforRateGridBO;
import com.future.model.bo.UserBO;
import com.future.model.po.DepartmentModel;
import com.future.model.po.InforRateModel;
import com.future.model.po.SecondFillBlankModel;
import com.future.model.po.UserModel;

public class InforRateGrid implements GenerateGrid<InforRateGridBO> {
	private static Logger logger = LogManager.getLogger(InforRateGrid.class);

	@Override
	public Map<Integer, InforRateGridBO> generateGrid(UserBO userBo) {
		Map<Integer, InforRateGridBO> result = new HashMap<Integer, InforRateGridBO>();

		UserDAO userDao = new UserDAO();
		DepartmentDAO departmentDao = new DepartmentDAO();
		SecondFillBlankDAO secondFillBlankDao = new SecondFillBlankDAO();

		UserModel currentUser = userBo.getUser();
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("areaid", currentUser.getAreaId());
		List<UserModel> userList = userDao.find(condition);
		InforRateGridBO inforRateGridBo = null;

		List<DepartmentModel> departmentList = departmentDao.findAll();
		Iterator<DepartmentModel> iter = departmentList.iterator();
		while (iter.hasNext()) {
			inforRateGridBo = new InforRateGridBO();
			DepartmentModel departmentModel = iter.next();
			if (departmentModel.getId() == 1 || departmentModel.getId() == 34||departmentModel.getId() == 35) {
				continue;
			}

			inforRateGridBo.setDepartmentId(departmentModel.getId());
			inforRateGridBo.setDepartmentName(departmentModel.getDepartment());

			Iterator<UserModel> userIter = userList.iterator();
			while (userIter.hasNext()) {
				UserModel user = userIter.next();
				if (user.getDepartmentId() == departmentModel.getId()) {
					Integer userId = user.getId();
					SecondFillBlankModel secondFillBlankModel = null;
					try {
						secondFillBlankModel = secondFillBlankDao.findByUserId(userId);
						Integer officesTotle = secondFillBlankModel.getItem0401();
						Integer supportInfotmationTotle = secondFillBlankModel.getItem0402();
						if (officesTotle < supportInfotmationTotle || officesTotle < 1 || officesTotle == null
								|| supportInfotmationTotle == null) {
							inforRateGridBo.setRate(0f);
						} else {
							float tempRate = (float) supportInfotmationTotle / (float) officesTotle * 100;
							inforRateGridBo.setRate((float) Math.round(tempRate * 100) / 100);
						}
					} catch (Exception e) {
						logger.error(e);
					}
					break;
				} else {
					inforRateGridBo.setRate(0f);
				}
			}
			result.put(departmentModel.getId(), inforRateGridBo);
		}
		return result;
	}

	@Override
	public List<InforRateGridBO> loadGrid(UserBO userBo) {
		List<InforRateGridBO> result = new ArrayList<InforRateGridBO>();
		DepartmentDAO dao = new DepartmentDAO();
		DepartmentModel department = null;
		InforRateDAO inforRateDao = new InforRateDAO();
		InforRateModel model = null;
		InforRateGridBO bo = null;
		List<InforRateModel> list = inforRateDao.findByUserId(userBo.getUser().getId());
		Iterator<InforRateModel> iter = list.iterator();
		while (iter.hasNext()) {
			bo = new InforRateGridBO();
			model = iter.next();
			bo.setDepartmentId(model.getDepartmentId());
			bo.setRate(model.getRate());

			try {
				department = dao.findById(model.getDepartmentId());
			} catch (Exception e) {
				logger.error(e);
			}
			bo.setDepartmentName(department.getDepartment());
			result.add(bo);
		}
		return result;
	}
}
